package pl.uz.zgora.plan;

import android.app.Activity;
import android.app.ProgressDialog;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView.OnEditorActionListener;

public class WidgetSettingsActivity extends Activity {

	private EditText redValue = null;
	private EditText greenValue = null;
	private EditText blueValue = null;
	private EditText alphaValue = null;
	private LinearLayout backgroundTester = null;
	private SeekBar aplhaBar = null;
	private SeekBar blueBar = null;
	private SeekBar redBar = null;
	private SeekBar greenBar = null;
	private int color = -1;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.widget_settings);
	    
	    redValue = (EditText) findViewById(R.id.widget_settings_red_value);
	    greenValue = (EditText) findViewById(R.id.widget_settings_green_value);
	    blueValue = (EditText) findViewById(R.id.widget_settings_blue_value);
	    alphaValue = (EditText) findViewById(R.id.widget_settings_alpha_value);
	    backgroundTester = (LinearLayout) findViewById(R.id.widget_settings_sample);
	    
	    setupUI();
	    updateColor();
	    
	    PrefrencesManager pm = PrefrencesManager.getInstance(this);
	    Integer color = pm.getIntFromPreferences("widget_bg_color");
	    if(color != -1) {
	    	setUIToColor(color);
	    }
	    
	    CheckBox stretchWidget = (CheckBox) findViewById(R.id.widget_settings_strech_checkbox);
	    Boolean doStretchWidget = pm.getBooleanFromPreferences("stretch_widget");
	    if(doStretchWidget) {
	    	stretchWidget.setChecked(true);
	    } else {
	    	stretchWidget.setChecked(false);
	    }
	}
	
	private void setUIToColor(final int color) {
		redBar.setProgress(Color.red(color));
		blueBar.setProgress(Color.blue(color));
		greenBar.setProgress(Color.green(color));
		aplhaBar.setProgress(Color.alpha(color));

		updateColor();
	}
	
	private void getCurrentColor() {
		final int alpha = Integer.valueOf(alphaValue.getText().toString());
		final int red = Integer.valueOf(redValue.getText().toString());
		final int green = Integer.valueOf(greenValue.getText().toString());
		final int blue = Integer.valueOf(blueValue.getText().toString());
		color = Color.argb(alpha, red, green, blue);
	}
	
	private void updateColor() {
		getCurrentColor();
		backgroundTester.setBackgroundColor(color);
	}
	
	private void setupRedColorUI() {
		redBar = (SeekBar) findViewById(R.id.widget_settings_red_bar);
	    redBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			public void onStartTrackingTouch(SeekBar seekBar) {}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				redValue.setText(String.valueOf(progress));
				updateColor();
			}
		});
	}
	
	private void setupGreenColorUI() {
		greenBar = (SeekBar) findViewById(R.id.widget_settings_green_bar);
	    greenBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			public void onStartTrackingTouch(SeekBar seekBar) {}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				greenValue.setText(String.valueOf(progress));
				updateColor();
			}
		});
	}
	
	private void setupBlueColorUI() {
		blueBar = (SeekBar) findViewById(R.id.widget_settings_blue_bar);
		blueBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			public void onStopTrackingTouch(SeekBar seekBar) {}

			public void onStartTrackingTouch(SeekBar seekBar) {}

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				blueValue.setText(String.valueOf(progress));
				updateColor();
			}
		});
	}
	
	private void setupAplhaColorUI() {
		aplhaBar = (SeekBar) findViewById(R.id.widget_settings_alpha_bar);
	    aplhaBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			public void onStartTrackingTouch(SeekBar seekBar) {}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				alphaValue.setText(String.valueOf(progress));
				updateColor();
			}
		});
	}
	
	private void setupUI() {
		setupRedColorUI();
	    
	    setupGreenColorUI();
	    
	    setupBlueColorUI();
	    
	    setupAplhaColorUI();
	    
	    CheckBox stretchWidget = (CheckBox) findViewById(R.id.widget_settings_strech_checkbox);
	    stretchWidget.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				PrefrencesManager pm = PrefrencesManager.getInstance(buttonView.getContext());
				if(isChecked) {
					pm.addBooleanToPreferences("stretch_widget", true);
				} else {
					pm.addBooleanToPreferences("stretch_widget", false);
				}
			}
		});
	    
	    Button saveBtn = (Button) findViewById(R.id.widget_setttings_save);
	    saveBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				PrefrencesManager pm = PrefrencesManager.getInstance(v.getContext());
				pm.addIntToPreferences("widget_bg_color", color);
				////
				AppWidgetManager appWidgetManager = AppWidgetManager.getInstance( v.getContext() );
				ComponentName thisWidget = new ComponentName(v.getContext(), NextClassWidget.class);
				int[] ids = appWidgetManager.getAppWidgetIds(thisWidget);
				if(ids != null) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   	
						for(int i = 0; i < ids.length; i++) {
							NextClassWidget.refreshWidgetUI(v.getContext(), ids[i]);
						}
				}
				////
				
				finish();
			}
		});
	    
	    Button restoreBtn = (Button) findViewById(R.id.widget_settings_restore);
	    restoreBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				redBar.setProgress(0);
				blueBar.setProgress(0);
				greenBar.setProgress(0);
				aplhaBar.setProgress(180);

				updateColor();
			}
		});
	    
	    OnKeyListener changeEditTextValue = new OnKeyListener() {
	    	public boolean onKey(View v, int keyCode, KeyEvent event) {

	    		if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
	    				(keyCode == KeyEvent.KEYCODE_ENTER)) {

	    			updateColor();

	    			updateColor();
	    			getCurrentColor();
	    			setUIToColor(color);

	    			return true;
	    		}
	    		return false;
	    	}
	    };
	    
	    alphaValue.setOnKeyListener(changeEditTextValue);
	    blueValue.setOnKeyListener(changeEditTextValue);
	    greenValue.setOnKeyListener(changeEditTextValue);
	    redValue.setOnKeyListener(changeEditTextValue);
	    
	    final Spinner widgetUpdateFreq = (Spinner) findViewById(R.id.widget_settings_update_freq);
	    widgetUpdateFreq.setSelection(4);
	    
	    final int selectedFreq = PrefrencesManager.getInstance(this).getIntFromPreferences("widget_update_freq");
	    if(selectedFreq > 0) {
	    		widgetUpdateFreq.setSelection(selectedFreq);
	    } else { widgetUpdateFreq.setSelection(0); }
	    widgetUpdateFreq.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				final Intent intent = new Intent(WidgetSettingsActivity.this, WidgetUpdateService.class);
				final PrefrencesManager pm = PrefrencesManager.getInstance(view.getContext());
				final ProgressDialog dialog = HelperMethodsManager.getProgressDialog(view.getContext(), "Poszę czekać ...");
				switch (pos) {
					case 0: {
						dialog.show();
						stopService(intent);
						WidgetUpdateService.setUpdateFreq(5);
						startService(intent);
						pm.addIntToPreferences("widget_update_freq", 0);
						dialog.dismiss();
					}
					break;
					
					case 1: {
						dialog.show();
						stopService(intent);
						WidgetUpdateService.setUpdateFreq(10);
						startService(intent);
						pm.addIntToPreferences("widget_update_freq", 1);
						dialog.dismiss();
					}
					break;
					
					case 2: {
						dialog.show();
						stopService(intent);
						WidgetUpdateService.setUpdateFreq(15);
						startService(intent);
						pm.addIntToPreferences("widget_update_freq", 2);
						dialog.dismiss();
					}
					break;
					
					case 3: {
						dialog.show();
						stopService(intent);
						WidgetUpdateService.setUpdateFreq(20);
						startService(intent);
						pm.addIntToPreferences("widget_update_freq", 3);
						dialog.dismiss();
					}
					break;
				
					case 4: {
						
						pm.addIntToPreferences("widget_update_freq", 4);
						stopService(intent);	
					}
					break;
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {}
		});
	}
}
